package com.hailiao.beidou.message.data.location

import android.annotation.SuppressLint
import com.d10ng.app.status.GnssStatusManager
import com.d10ng.app.status.LocationStatusManager
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.stateIn

/**
 * 位置信息仓库
 * @Author d10ng
 * @Date 2023/11/17 16:31
 */
object LocationRepository {

    // 当前位置信息
    val locationFlow = LocationStatusManager.statusFlow
        .combine(GnssStatusManager.statusFlow) { location, gnssStatus ->
            location?.toLocationInfo()?.apply {
                this.satelliteCount = gnssStatus?.satelliteCount ?: 0
            }
        }
        .stateIn(CoroutineScope(Dispatchers.IO), SharingStarted.Eagerly, null)

    /**
     * 开启定位查询
     */
    @SuppressLint("MissingPermission")
    fun start() {
        GnssStatusManager.start()
        LocationStatusManager.start()
    }

    /**
     * 停止定位查询
     */
    fun stop() {
        GnssStatusManager.stop()
        LocationStatusManager.stop()
    }
}